# encoding=utf-8
from storages import cache, database
from constant.const import const
import json


def update():
    d = database.database()
    count = 'select count(id) from ' + d.database + '.book.' + const.chapter
    d_execute = d.execute(count)[0][0]
    num = int(d_execute / 10000) + 1
    for i in range(num):
        select = "select jsonb_array_length(\"content\") as count , id from " + d.database + ".book." + const.chapter + " limit 10000 offset " + str(i)
        execute = d.execute(select)
        for (count, id) in execute:
            print(str(count) + " " + id)
            for i in range(count):
                update = "update " + d.database + ".book." + const.chapter + " set tag = jsonb_set(tag,'{" + str(
                    i) + ",flag}','0') where id = '" + id + "'"
                d.execute(update)
                break


# update()

def delete_all_data():
    cache.cache().string("k", 1, 0, const.px)
    d = database.database()
    d.execute("delete from self.book.author ")
    d.execute("delete from self.book.book ")
    d.execute("delete from self.book.chapter ")


# delete_all_data()


def get():
    d = database.database()
    execute = d.execute("select id from self.book.author where author_name = '淡月新凉'")[0][0]
    print(execute)


get()
